﻿// <copyright file="HoldingSource.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System;
    using System.Linq;
    using LightSwitchApplication.Rules;
    using LightSwitchApplication.UserCode;

    public partial class HoldingSource
    {
        #region Summary

        partial void GMSummary_Compute(ref string result)
        {
            string ownerString = (this.Owner != null) ? this.Owner.Abbreviation : "None";
            string effects = (this.IsContested) ? "C" : string.Empty;

            result = string.Format("{0} ({1}{2})", ownerString, this.Level, effects);
        }

        partial void PublicSummary_Compute(ref string result)
        {
            string ownerString = (this.Owner != null) ? this.Owner.Abbreviation : "None";
            string effects = (this.IsContested) ? "C" : string.Empty;

            result = string.Format("{0} ({1}{2})", ownerString, Utilities.MaskValue(this.Level), effects);
        }

        partial void GMSummaryForLeyLines_Compute(ref string result)
        {
            if (this.Province != null)
            {
                result = string.Format("{0} - {1} - {2} - {3}/{4} ({5}/{6} LLs)",
                    this.Province.FullName,
                    (this.Province.Domain != null) ? this.Province.Domain.Name : "-",
                    this.GMSummary,
                    this.Province.BaseProvince.Terrain.Name,
                    (this.Province.Race != null) ? this.Province.Race.Name : "-",
                    this.HoldingNumberLeyLines,
                    this.OwnerNumberLeyLines);
            }
        }

        #endregion

        #region Number Ley Lines

        partial void HoldingNumberLeyLines_Compute(ref int result)
        {
            result = this.LeyLinesStarting.Count() + this.LeyLinesEnding.Count();
        }

        partial void OwnerNumberLeyLines_Compute(ref int result)
        {
            var llsStart = this.Province.SourceHoldings
                                    .SelectMany(h => h.LeyLinesStarting)
                                    .Where(ll => ll.Visibility == (int)VisibilityEnum.Public ||
                                                 (ll.Visibility == (int)VisibilityEnum.Owner && ll.StartPoint == this));

            var llsEnd = this.Province.SourceHoldings
                                    .SelectMany(h => h.LeyLinesEnding)
                                    .Where(ll => ll.Visibility == (int)VisibilityEnum.Public ||
                                                (ll.Visibility == (int)VisibilityEnum.Owner && ll.EndPoint != null && ll.EndPoint == this));

            result = llsStart
                        .Concat(llsEnd)
                        .Distinct()
                        .Count();
        }

        #endregion

        #region HasInformation

        partial void HasPublicInformation_Compute(ref bool result)
        {
            result = !string.IsNullOrEmpty(this.PublicInformation);
        }

        partial void HasGMInformation_Compute(ref bool result)
        {
            result = !string.IsNullOrEmpty(this.GMInformation);
        }

        partial void HasOwnerInformation_Compute(ref bool result)
        {
            result = !string.IsNullOrEmpty(this.OwnerInformation);
        }

        #endregion        
    }
}
